// pages/store/store.js
const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendOrder:[],
    receiveOrder:[],
    sushelou:'',
    gender:'',

    tab: [{
      title: '代取快递',
      id: 0
     },
     {
      title: '代寄快递',
      id: 1
     },
     ],

     idx: 0, //默认选中第一项
  },

  // tab
  tab(e) {
      let that = this;
      let index = e.currentTarget.dataset.index;
      that.setData({
      idx: index,
      })
    
    },

  finish(e){
    //console.log('dianji',e)
    wx.cloud.callFunction({
      name:'storerUpdateRS',
      data:e.currentTarget.dataset,
      success(res){
        console.log('点击完成成功',res)

      },
      fail(err){
        console.log('点击完成失败',err)
      }
    })
  },

  refresh(){
    var that = this;
    db.collection('ReceiveAndSend').orderBy('date', 'desc').orderBy('state','desc').where({
      sushelou:this.data.sushelou,
      gender:this.data.gender,
      type:"代寄快递"
    }).get({
      success:function(res){
        console.log('快递员获取用户寄件订单成功',res);
          that.setData({
            sendOrder:res.data
          })
      },
      fail:function(err){
        console.log('快递员获取用户寄件订单失败',err);
          wx.showToast({
            title: '获取数据失败',
            icon:'error'
          })
      },
    })

    db.collection('ReceiveAndSend').where({
      sushelou:this.data.sushelou,
      gender:this.data.gender,
      type:"代取快递"
    }).get({
      success:function(res){
        console.log('快递员获取用户取件订单成功',res);
          that.setData({
            receiveOrder:res.data
          })
      },
      fail:function(err){
        console.log('快递员获取用户取件订单失败',err);
          wx.showToast({
            title: '获取数据失败',
            icon:'error'
          })
      },
    })
  },

  // finishReceive(e){

  // }

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const storer=wx.getStorageSync("storer");
    var arr = storer.zhanghu.split(".");
    console.log('arr的值为：',arr);
    this.setData({
      gender:arr[0],
      sushelou:arr[1],
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    db.collection('ReceiveAndSend').orderBy('date', 'desc').orderBy('state','desc').where({
      sushelou:this.data.sushelou,
      gender:this.data.gender,
      type:"代寄快递"
    }).get({
      success:function(res){
        console.log('快递员获取用户寄件订单成功',res);
          that.setData({
            sendOrder:res.data
          })
      },
      fail:function(err){
        console.log('快递员获取用户寄件订单失败',err);
          wx.showToast({
            title: '获取数据失败',
            icon:'error'
          })
      },
    })

    db.collection('ReceiveAndSend').where({
      sushelou:this.data.sushelou,
      gender:this.data.gender,
      type:"代取快递"
    }).get({
      success:function(res){
        console.log('快递员获取用户取件订单成功',res);
          that.setData({
            receiveOrder:res.data
          })
      },
      fail:function(err){
        console.log('快递员获取用户取件订单失败',err);
          wx.showToast({
            title: '获取数据失败',
            icon:'error'
          })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})